package com.xbongbong.workflow.config;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import com.xbongbong.workflow.service.operator.WorkflowOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 吴峰
 * @date 2021/06/18 16:41
 */
@Configuration
public class WorkflowNodeOperatorFieldConfig {

    @Autowired
    private Map<String, WorkflowOperatorFieldHandler> workflowOperatorFieldMap;

    @Autowired
    private Map<String, WorkflowOperatorService> workflowOperatorMap;


    @Bean("workflowOperatorFieldMap")
    public Map<Integer, WorkflowOperatorFieldHandler> initOperatorFieldMap() {
        Map<Integer, WorkflowOperatorFieldHandler> workflowOperatorFieldMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.workflowOperatorFieldMap.forEach((key, item)->{
            item.type().forEach(fieldType->{
                workflowOperatorFieldMap.put(fieldType, item);
            });
        });

        return workflowOperatorFieldMap;
    }

    @Bean("workflowOperatorMap")
    public Map<Integer, WorkflowOperatorService> initOperatorMap() {
        return workflowOperatorMap.values().stream().collect(Collectors.toMap(WorkflowOperatorService::type, t -> t));
    }
}
